package com.zxl.grocery.core.webmagic.scheduled;

import com.zxl.grocery.common.persistence.model.CrawlerConfig;
import com.zxl.grocery.core.webmagic.pipeline.NovelPipeline;
import com.zxl.grocery.core.webmagic.processor.BiQuGeProcessor;
import com.zxl.grocery.core.webmagic.processor.DevelopersProcessor;
import com.zxl.grocery.modular.crawler.service.ICrawlerConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * info:小说定时任务
 * Created by zxl on 2017/8/8 0008.
 */
@Component
@EnableScheduling
public class NovelScheduled {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private NovelPipeline novelPipeline;
    @Autowired
    private ICrawlerConfigService iCrawlerConfigService;

    public static final String shengXuUrl = "http://www.biqiuge.com/book/4772/";

    public static final String yiNianYongHeng = "http://www.biqiuge.com/book/630/";

    public static final String woShiZhiZun = "http://www.biqiuge.com/book/2956/";

    /**
     * 笔趣阁
     */
    ////[秒] [分] [小时] [日] [月] [周] [年]
    @Scheduled(cron = "0 33 * * * ? ")//从0点开始,每3个小时执行一次
    public void biQuGeScheduled() {
        HashMap<String,Object> conditionMap = new HashMap<String,Object>();
        conditionMap.put("code","BiQuGeScheduled");
        List<CrawlerConfig> crawlerConfigs = iCrawlerConfigService.selectByMap(conditionMap);

        if (crawlerConfigs.size() > 0 ) {
            for (int i = 0; i < crawlerConfigs.size(); i++) {
                if (crawlerConfigs.get(i).getContent().equals("1")){
                    System.out.println("----开始执行笔趣阁----定时任务");
                    Spider spider= Spider.create(new BiQuGeProcessor());
 //                  spider.addUrl(shengXuUrl);
                    spider.addUrl(yiNianYongHeng,shengXuUrl,woShiZhiZun);
                    spider.addPipeline(novelPipeline);
                    spider.thread(5);
                    spider.setExitWhenComplete(true);
                    spider.start();
                    spider.stop();
                }
            }

        }
    }
}
